﻿namespace Redux.Cryptography
{
    public class AuthCryptography 
    {
        private ushort InCounter;
        private ushort OutCounter;

        public void Decrypt(byte[] In, byte[] Out, int Length)
        {
            lock (this)
            {
                for (int i = 0; i < Length; i++)
                {
                    Out[i] = (byte)(In[i] ^ 0xab);
                    Out[i] = (byte)((Out[i] << 4) | (Out[i] >> 4));
                    Out[i] = (byte)(Key2[this.InCounter >> 8] ^ Out[i]);
                    Out[i] = (byte)(Key1[this.InCounter & 0xff] ^ Out[i]);
                    this.InCounter = (ushort)(this.InCounter + 1);
                }
            }
        }

        public unsafe void Encrypt(byte* In, byte[] Out, int Length)
        {
            lock (this)
            {
                for (int i = 0; i < Length; i++)
                {
                    Out[i] = (byte)(In[i] ^ 0xab);
                    Out[i] = (byte)((Out[i] << 4) | (Out[i] >> 4));
                    Out[i] = (byte)(Key2[this.OutCounter >> 8] ^ Out[i]);
                    Out[i] = (byte)(Key1[this.OutCounter & 0xff] ^ Out[i]);
                    this.OutCounter = (ushort)(this.OutCounter + 1);
                }
            }
        }

        public void Encrypt(byte[] In, byte[] Out, int Length)
        {
            lock (this)
            {
                for (int i = 0; i < Length; i++)
                {
                    Out[i] = (byte)(In[i] ^ 0xab);
                    Out[i] = (byte)((Out[i] << 4) | (Out[i] >> 4));
                    Out[i] = (byte)(Key2[this.OutCounter >> 8] ^ Out[i]);
                    Out[i] = (byte)(Key1[this.OutCounter & 0xff] ^ Out[i]);
                    this.OutCounter = (ushort)(this.OutCounter + 1);
                }
            }
        } 
        
        #region  Key 1
        public static byte[] Key1 = new byte[] { 
            0x9d, 0x90, 0x83, 0x8a, 0xd1, 140, 0xe7, 0xf6, 0x25, 40, 0xeb, 130, 0x99, 100, 0x8f, 0x2e, 
            0x2d, 0x40, 0xd3, 250, 0xe1, 0xbc, 0xb7, 230, 0xb5, 0xd8, 0x3b, 0xf2, 0xa9, 0x94, 0x5f, 30, 
            0xbd, 240, 0x23, 0x6a, 0xf1, 0xec, 0x87, 0xd6, 0x45, 0x88, 0x8b, 0x62, 0xb9, 0xc4, 0x2f, 14, 
            0x4d, 160, 0x73, 0xda, 1, 0x1c, 0x57, 0xc6, 0xd5, 0x38, 0xdb, 210, 0xc9, 0xf4, 0xff, 0xfe, 
            0xdd, 80, 0xc3, 0x4a, 0x11, 0x4c, 0x27, 0xb6, 0x65, 0xe8, 0x2b, 0x42, 0xd9, 0x24, 0xcf, 0xee, 
            0x6d, 0, 0x13, 0xba, 0x21, 0x7c, 0xf7, 0xa6, 0xf5, 0x98, 0x7b, 0xb2, 0xe9, 0x54, 0x9f, 0xde, 
            0xfd, 0xb0, 0x63, 0x2a, 0x31, 0xac, 0xc7, 150, 0x85, 0x48, 0xcb, 0x22, 0xf9, 0x84, 0x6f, 0xce, 
            0x8d, 0x60, 0xb3, 0x9a, 0x41, 220, 0x97, 0x86, 0x15, 0xf8, 0x1b, 0x92, 9, 180, 0x3f, 190, 
            0x1d, 0x10, 3, 10, 0x51, 12, 0x67, 0x76, 0xa5, 0xa8, 0x6b, 2, 0x19, 0xe4, 15, 0xae, 
            0xad, 0xc0, 0x53, 0x7a, 0x61, 60, 0x37, 0x66, 0x35, 0x58, 0xbb, 0x72, 0x29, 20, 0xdf, 0x9e, 
            0x3d, 0x70, 0xa3, 0xea, 0x71, 0x6c, 7, 0x56, 0xc5, 8, 11, 0xe2, 0x39, 0x44, 0xaf, 0x8e, 
            0xcd, 0x20, 0xf3, 90, 0x81, 0x9c, 0xd7, 70, 0x55, 0xb8, 0x5b, 0x52, 0x49, 0x74, 0x7f, 0x7e, 
            0x5d, 0xd0, 0x43, 0xca, 0x91, 0xcc, 0xa7, 0x36, 0xe5, 0x68, 0xab, 0xc2, 0x59, 0xa4, 0x4f, 110, 
            0xed, 0x80, 0x93, 0x3a, 0xa1, 0xfc, 0x77, 0x26, 0x75, 0x18, 0xfb, 50, 0x69, 0xd4, 0x1f, 0x5e, 
            0x7d, 0x30, 0xe3, 170, 0xb1, 0x2c, 0x47, 0x16, 5, 200, 0x4b, 0xa2, 0x79, 4, 0xef, 0x4e, 
            13, 0xe0, 0x33, 0x1a, 0xc1, 0x5c, 0x17, 6, 0x95, 120, 0x9b, 0x12, 0x89, 0x34, 0xbf, 0x3e
         };
        #endregion
        #region Key 2
        public static byte[] Key2 = new byte[] { 
            0x62, 0x4f, 0xe8, 0x15, 0xde, 0xeb, 4, 0x91, 0x1a, 0xc7, 0xe0, 0x4d, 0x16, 0xe3, 0x7c, 0x49, 
            210, 0x3f, 0xd8, 0x85, 0x4e, 0xdb, 0xf4, 1, 0x8a, 0xb7, 0xd0, 0xbd, 0x86, 0xd3, 0x6c, 0xb9, 
            0x42, 0x2f, 200, 0xf5, 190, 0xcb, 0xe4, 0x71, 250, 0xa7, 0xc0, 0x2d, 0xf6, 0xc3, 0x5c, 0x29, 
            0xb2, 0x1f, 0xb8, 0x65, 0x2e, 0xbb, 0xd4, 0xe1, 0x6a, 0x97, 0xb0, 0x9d, 0x66, 0xb3, 0x4c, 0x99, 
            0x22, 15, 0xa8, 0xd5, 0x9e, 0xab, 0xc4, 0x51, 0xda, 0x87, 160, 13, 0xd6, 0xa3, 60, 9, 
            0x92, 0xff, 0x98, 0x45, 14, 0x9b, 180, 0xc1, 0x4a, 0x77, 0x90, 0x7d, 70, 0x93, 0x2c, 0x79, 
            2, 0xef, 0x88, 0xb5, 0x7e, 0x8b, 0xa4, 0x31, 0xba, 0x67, 0x80, 0xed, 0xb6, 0x83, 0x1c, 0xe9, 
            0x72, 0xdf, 120, 0x25, 0xee, 0x7b, 0x94, 0xa1, 0x2a, 0x57, 0x70, 0x5d, 0x26, 0x73, 12, 0x59, 
            0xe2, 0xcf, 0x68, 0x95, 0x5e, 0x6b, 0x84, 0x11, 0x9a, 0x47, 0x60, 0xcd, 150, 0x63, 0xfc, 0xc9, 
            0x52, 0xbf, 0x58, 5, 0xce, 0x5b, 0x74, 0x81, 10, 0x37, 80, 0x3d, 6, 0x53, 0xec, 0x39, 
            0xc2, 0xaf, 0x48, 0x75, 0x3e, 0x4b, 100, 0xf1, 0x7a, 0x27, 0x40, 0xad, 0x76, 0x43, 220, 0xa9, 
            50, 0x9f, 0x38, 0xe5, 0xae, 0x3b, 0x54, 0x61, 0xea, 0x17, 0x30, 0x1d, 230, 0x33, 0xcc, 0x19, 
            0xa2, 0x8f, 40, 0x55, 30, 0x2b, 0x44, 0xd1, 90, 7, 0x20, 0x8d, 0x56, 0x23, 0xbc, 0x89, 
            0x12, 0x7f, 0x18, 0xc5, 0x8e, 0x1b, 0x34, 0x41, 0xca, 0xf7, 0x10, 0xfd, 0xc6, 0x13, 0xac, 0xf9, 
            130, 0x6f, 8, 0x35, 0xfe, 11, 0x24, 0xb1, 0x3a, 0xe7, 0, 0x6d, 0x36, 3, 0x9c, 0x69, 
            0xf2, 0x5f, 0xf8, 0xa5, 110, 0xfb, 20, 0x21, 170, 0xd7, 240, 0xdd, 0xa6, 0xf3, 140, 0xd9
         };

        #endregion
    }
}
